#-*-perl-*-
use Test::More qw(no_plan);
use Module::Build;
use lib '../lib';
use strict;
use warnings;

my $build = Module::Build->current;
my $badport = 30000;
my ($host,$port,$pass);

use_ok 'AlienArena::RCon';

my $pfile_exists = open my $p, 'p';
if ($pfile_exists){
    my $l = <$p>;
    ($host,$port,$pass) = split /\t/,$l;
}
elsif ($build) {
    ($host,$port,$pass) = ($build->notes('test_host'),
			   $build->notes('test_port'),
			   $build->notes('test_pass'));
}
else {
    ($host, $port, $pass) = @ENV{qw(AA_HOST AA_PORT AA_PASS)};
}
my $do_live_tests = ($host && $port && $pass);
SKIP : {
    skip 'live tests not requested', 11 unless $do_live_tests;
    ok my $console = AlienArena::RCon::Console->init(
	Host => "$host:$port",
	Pass => $pass,
	Retry => 1,
	Timeout => 5
	);
    isa_ok $console, 'AlienArena::RCon::Console';
    is $console->host, $host, 'host ip parsed';
    is $console->port, $port, 'port parsed';
    ok $console->send_command('skill'), 'send one command';
    isa_ok $console->response, 'AlienArena::RCon::Response';
    like $console->response_as_text, qr/skill/, 'response to skill command received';
    ok $console->send_command('sv_botkickthreshold'), 'send another command';
    like $console->response_as_text, qr/kickthreshold/, 'response to command received';
    $console->port($badport);
    $console->send_command('skill');
    ok !$console->ok, 'bad port returns error';
    like $console->errmsg, qr/timed out/i, 'timeout errmsg';
}



1;
